﻿@{
    ViewBag.Title = "运营数据统计";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

@using ST = EmoneyWeiXinCommon.StringTools;
<script type="text/javascript" src="@ST.GetStaticFile("My97DatePicker/WdatePicker.js")"></script>

<div class="right">
    <h3>运营统计数据</h3>
    <!--manage-->
    <div class="manage">
        <!--manage_top-->
        <div class="manage_top">
            <ul class="manage_top_ul">
            </ul>
            <div class="manage_register">
                @{
                    DateTime today = DateTime.Now.AddDays(1);
                    DateTime yesterday = today.AddDays(-30);
                }
                <p><span>发布时间：</span><input type="text" class="manage_register_text" onfocus="WdatePicker()" id="txt_startTime" value="@yesterday.ToString("yyyy-MM-dd")"><span class="manage_register_font">至</span><input type="text" class="manage_register_text" onfocus="WdatePicker()" id="txt_endTime"  value="@today.ToString("yyyy-MM-dd")"/></p>
                <p>
                    <span>统计维度：</span><select id="selwType">
                        <option value="3">栏目</option>
                        <option value="2">关键字</option>
                    </select>
                </p>
            </div>
            <p class="manage_add">
                <a href="#" id="btnSearch">查询</a>
                <a href="#" class="manage_add_font">重置</a>
            </p>
        </div>
        <!--manage_top end-->
        <p class="manage_line"></p>
        <!--manage_bottom-->
        <div class="manage_bottom">
            <!--manage_bottom_title-->
            <div class="manage_bottom_title">
                <p>累计用户人次：<span class="red" id="spSumPv">0</span>人次    累计人数：<span class="red" id="spSumUv">0</span>人</p>
            </div>
            <!--manage_bottom_title--end-->
            <table border="0" cellpadding="0" cellspacing="0" width="100%" id="dataTable">
                <tr>
                    <th><span>编号</span></th>
                    <th><span>统计维度（栏目/关键字）</span></th>
                    <th><span>人数</span></th>
                    <th><span>次数</span></th>
                </tr>
                @*<tr>
                    <td><span>001</span></td>
                    <td><span>10010@chaoguquan</span></td>
                    <td><span>emoney.cn</span></td>
                    <td><span>2015/01/26  14:33:32</span></td>
                </tr>
                <tr>
                    <td><span>002</span></td>
                    <td><span>10010@chaoguquan</span></td>
                    <td><span>emoney.cn</span></td>
                    <td><span>2015/01/26  14:33:32</span></td>
                </tr>
                <tr>
                    <td><span>003</span></td>
                    <td><span>10010@chaoguquan</span></td>
                    <td><span>emoney.cn</span></td>
                    <td><span>2015/01/26  14:33:32</span></td>
                </tr>*@

            </table>
            <div class="manage_page" id="manage_page">
            </div>
        </div>
        <!--manage_bottom end-->
    </div>
    <!--manage end-->
</div>

<div style="width: 100%; height: 100%; display: none; position: absolute; top: 0px; left: 0px;" id="loadDiv">
    <div id="subLoadDiv">
        <img src="@Url.Content("~/Static/Image/load.gif")" />
    </div>
</div>

<script type="text/javascript">
    var weixinid = "";
    var pageIndex = 1;
    var pageSize = 24;
    var pageCount = 1;
    var totalCount = 0;
    var getUserMenuUrl = "@ST.GetTrendsFile("BaseConfiguration/WeixinUserMemu")" + "?random=" + Math.random();
    var getTJBusinessDataUrl = "@ST.GetTrendsFile("TJBusiness/GetMotionData")";
    var viewBagWeixinId = "@ViewBag.weixinid";
    $(document).ready(function () {
        createMemu();
        initEvent();

    });
    function showLoading() {
        var margintop = (document.documentElement.clientHeight) / 2 - 20;
        var marginleft = (document.documentElement.clientWidth) / 2 - 20;
        $("#subLoadDiv").css({ "margin-top": margintop, "margin-left": marginleft });
        $("#loadDiv").show();
    }

    function hideLoading() {
        $("#loadDiv").hide();
    }

    function getContentMemu() {
        $(".manage_top_ul .manage_top_A").each(function () {
            weixinid = $(this).attr("typeid");
        })
    }
    //创建labs
    function createMemu() {
        $.ajax({
            type: "get",
            url: getUserMenuUrl,
            async: false,
            data: { weixinid: viewBagWeixinId },
            dataType: "html",
            success: function (result) {
                $(".manage_top_ul").html(result);
                $(".manage_top_ul li").click(function () {
                    $(".manage_top_ul li").removeClass("manage_top_A");
                    $(this).addClass("manage_top_A");
                    getContentMemu();
                    getMotionData(1);


                })
                getContentMemu();
            }
        });
    }

    function render(result) {
        totalCount = result.totalCount;
        PageCountHtml();
        $("#spSumPv").text(result.SumPv);
        $("#spSumUv").text(result.SumUv);
        var dataList = result.DataList;
        var dataTable = $("#dataTable");
        dataTable.find("tr:gt(0)").remove();
        for (var i = 0; i < dataList.length; i++) {
            var theData = dataList[i];
            var $newRow = createRow(theData);
            dataTable.append($newRow);
        }

    }

    function createRow(rowData) {
        var $newRow = $("<tr><td>" + rowData.ID + "</td><td>" + rowData.ItemName + "</td><td>" + rowData.PV + "</td><td>" + rowData.UV + "</td></tr>");
        return $newRow;
    }

    //获取数据
    function getMotionData(rsearch) {
        if (rsearch == 1) {
            pageIndex = 1;
        }
        var startTime = $("#txt_startTime").val();
        var endTime = $("#txt_endTime").val();
        var wType = $("#selwType").val();
        var postData = { size: pageSize, index: pageIndex, startTime: startTime, endTime: endTime, weixinID: weixinid, wType: wType, research: rsearch };
        var dataResult;
        $.ajax({
            type: "post",
            url: getTJBusinessDataUrl,
            beforeSend: function () {
                showLoading();
            },
            complete: function () {
                hideLoading();
            },
            data: postData,
            dataType: "json",
            success: function (result) {
                render(result);
                // dataResult = result;
                //alert(dataResult);
            }
        });
        return dataResult;
    }

    function initEvent() {
        $("#btnSearch").click(function () {
            getMotionData(1);

        });
    }


    //翻页
    function PageCountHtml() {
        pageCount = Math.ceil(totalCount / pageSize);
        if (pageCount == 0) {
            pageCount = 1;
        }
        $("#manage_page").html(PageHtml(pageIndex, pageCount));
        $("#manage_page .manage_page_right").on("click", function () {
            pageIndex = pageIndex + parseInt($(this).attr("tname"));
            getMotionData();

        });

        $("#manage_page .tiaozhuan").click(function () {
            var pageNum = $("#txtPageNum").val();
            var tempIndex;
            try {
                tempIndex = parseInt(pageNum);
            } catch (e) {
                alert("请输入数字");
                return false;
            }

            if (pageNum > 0 && pageNum <= pageCount) {
                pageIndex = tempIndex;
                $("#manage_page").html(PageHtml(pageIndex, pageCount));
                getMotionData();
            }
            else {
                alert("当前数字超出范围");
                return false;
            }

        });

    }

</script>
